//! refactored_servers_test.rs 单元测试
//! 
//! 测试重构后的服务器实现

use redis_rs2::server::refactored::*;
use redis_rs2::server::enhanced_traits::*;
use tokio_test;

#[cfg(test)]
mod refactored_standalone_server_tests {
    use super::*;
    
    #[tokio::test]
    async fn test_refactored_standalone_server_creation() {
        let result = RefactoredStandaloneServer::new("127.0.0.1:16379".to_string()).await;
        assert!(result.is_ok());
        
        let server = result.unwrap();
        assert_eq!(server.bind_address(), "127.0.0.1:16379");
        assert!(!server.is_running());
        assert_eq!(server.config().mode(), redis_rs2::storage::RedisServerMode::Standalone);
    }
    
    #[tokio::test]
    async fn test_refactored_standalone_server_from_config() {
        let config = Box::new(redis_rs2::server::enhanced_traits::StandaloneConfig {
            bind_address: "127.0.0.1:16380".to_string(),
        });
        
        let result = RefactoredStandaloneServer::from_config(config, None).await;
        assert!(result.is_ok());
        
        let server = result.unwrap();
        assert_eq!(server.bind_address(), "127.0.0.1:16380");
    }
    
    #[tokio::test]
    async fn test_refactored_standalone_server_invalid_config() {
        let invalid_config = Box::new(redis_rs2::server::enhanced_traits::ClusterConfig {
            bind_address: "127.0.0.1:7001".to_string(),
            cluster_config: redis_rs2::cluster::ClusterConfig {
                node_id: "test".to_string(),
                redis_port: 7001,
                cluster_port: 17001,
                seeds: vec![],
            },
        });
        
        let result = RefactoredStandaloneServer::from_config(invalid_config, None).await;
        assert!(result.is_err()); // 应该拒绝集群配置
    }
    
    #[tokio::test]
    async fn test_standalone_server_adapter() {
        let result = StandaloneServerAdapter::new("127.0.0.1:16381".to_string()).await;
        assert!(result.is_ok());
        
        let adapter = result.unwrap();
        assert!(!adapter.is_running());
        assert_eq!(adapter.get_mode(), redis_rs2::storage::RedisServerMode::Standalone);
    }
}

#[cfg(test)]
mod convenience_functions_tests {
    use super::*;
    
    #[tokio::test]
    async fn test_create_production_standalone_server() {
        let result = create_production_standalone_server("127.0.0.1:16382".to_string()).await;
        assert!(result.is_ok());
        
        let server = result.unwrap();
        assert_eq!(server.bind_address(), "127.0.0.1:16382");
    }
    
    #[cfg(test)]
    #[tokio::test]
    async fn test_create_test_standalone_server() {
        let result = create_test_standalone_server("127.0.0.1:16383".to_string()).await;
        assert!(result.is_ok());
        
        let server = result.unwrap();
        assert_eq!(server.bind_address(), "127.0.0.1:16383");
    }
}